<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<script type="text/javascript" src="<?php echo base_url()?>js/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="<?php echo base_url()?>js/jquery.md5.js"></script>
<script type="text/javascript" >
    jQuery(document).ready(function(){
        var baseUrl = "<?php echo base_url() ?>"
        $("#login").unbind("click").bind("click",function(){
            var username = $("#username").val();
            if(username.length == 0){
                mess = "Phải nhập tên tài khoản";
                $("#error_message").text(mess);
                return ;
            }
            var password = $("#password").val();
            password = $.md5(password);

            var array = [];
            array.push("username="+username);
            array.push("&password="+password);
            $.ajax({
                type: "POST",
                url:baseUrl+"login/login_user",
                data: array.join(""),
                dataType: "json",
                async: false,
                beforeSend : function(){
                    $("#error_message").html("LOADDING....");
                },
                success: function(json) {
                    status = json.status;
                    message = json.message;
                    openid = json.id;
                    openkey = json.session;
                    if(status == "true"){
                        document.location = "http://123.30.214.243/?openid="+openid+"&openkey="+openkey;
                    }else{
                        $("#error_message").html(message);
                    }
                }
            })
        });
        
        $("#register").unbind("click").bind("click",function(){
            document.location = baseUrl +"register";
        })
        
        $("#logout").unbind("click").bind("click",function(){
            $.ajax({
                type: "POST",
                url:baseUrl+"login/logout_user",
                dataType: "json",
                async: false,
                beforeSend : function(){
                    $("#error_message").html("LOADDING....");
                },
                success: function(json) {
                    status = json.status;
                    message = json.message;
                    alert(message);
                    $("#error_message").html(message);
                }
            })
        })
    })

</script>
<title>Untitled Document</title> 
</head> 
    <body>
        <label for="username">Username:</label>
        <input type="text" size="20" id="username" name="username"/>
        <br/>
        
        <label for="password">Password:</label>
        <input type="password" size="20" id="password" name="password"/>
        <br/>
        
        <label id="error_message" style="color: red"></label><br/>
        
        <input type="button" value="Đăng nhập" id ="login"/>
        <input type="button" value="Đăng ký mới" id ="register"/>
        <input type="button" value="Thoát" id ="logout"/>
       
        <?php
        
//        echo validation_errors();
//        echo form_open('login/index/');
//        echo form_label('Nhập tài khoản:');
//        echo form_input('username').'<br/>';
//        echo form_error('username'); 
//        echo form_label('Nhập mật khẩu');
//        echo form_password('password').'<br />';
//        echo form_error('password');   
//        echo form_submit('ok','Đăng nhập');
//        echo form_fieldset_close();
//        echo form_close();
        
//        if(isset($data)){
//            echo $data['openkey'];
//        }
//        if(!is_null($data)){
//            print_r($data['openid']);
//        }
        ?>      
    </body> 
</html>